package com.snimay.drools;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.web.bind.annotation.GetMapping;
import com.google.gson.Gson;
import com.snimay.drools.config.DroolsAutoConfiguration;
import com.snimay.drools.vo.Address;
import com.snimay.drools.vo.AddressCheckResult;
import com.snimay.util.ToWeb;

//@RestController
//@RequestMapping("drools")
public class DroolsController {
	@Resource
	private KieContainer kieContainer;
	@GetMapping("/")
	public Object getOne() throws IOException {
		List<String> lis= new ArrayList<String>();
		Address address = new Address();
        address.setPostcode("99425");
      //  kieSession.destroy();
       // kieSession.
        KieSession kieSession = new DroolsAutoConfiguration().kieContainer().newKieSession();
        AddressCheckResult result = new AddressCheckResult();
        kieSession.insert(address);
        kieSession.insert(result);
        int ruleFiredCount = kieSession.fireAllRules();
		
		
		
        lis.add("触发了" + ruleFiredCount + "条规则");
        if(result.isPostCodeResult()){
            lis.add("规则校验通过");
        }
      // kieSession.dispose();  
        kieSession.destroy();
		ToWeb toweb = new ToWeb();
		
		
		
		toweb.setObjData(lis);
		return new Gson().toJson(toweb);
	}
}
